﻿using D.Wx.Dtos;
using D.Wx.Entities;
using D.Wx.Filter;
using D.Wx.IRepository;
using Microsoft.AspNetCore.Mvc;

namespace D.Wx
{
    /// <summary>
    /// 管理员模块
    /// </summary>
    [Route("api/sysAdmin")]
    public class SysAdminController : BaseApiController
    {

        private readonly ISysAdminEntityRepository _sysAdminRepository;

        /// <summary>
        /// 构造注入
        /// </summary>
        /// <param name="sysAdminRepository"></param>
        public SysAdminController(ISysAdminEntityRepository sysAdminRepository)
        {
            this._sysAdminRepository = sysAdminRepository;
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet("queryPage"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<PageModel<AdminEntityOutput>>> QueryPage([FromQuery] SysAdminEntityPageQuery query)
        {
            return new ApiResult<PageModel<AdminEntityOutput>>() { Data = await _sysAdminRepository.QueryPage(query) };
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<string>> Add([FromBody] SysAdminEntity input)
        {
            input.CreateUser = Account;
            input.AssignTotalCount = 0;
            var dbres = await _sysAdminRepository.Add(input);
            var res = new ApiResult<string>
            {
                Msg = dbres > 0 ? "新增成功" : "新增失败"
            };
            return res;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("delete"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<string>> Delete([FromBody] List<string> ids)
        {
            var dbres = await _sysAdminRepository.Delete(ids);
            var res = new ApiResult<string>
            {
                Msg = dbres > 0 ? "删除成功" : "删除失败"
            };
            return res;
        }

        /// <summary>
        /// 根据ID主键获取详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("get"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<AdminEntityOutput>> Get([FromQuery] string id)
        {
            return new ApiResult<AdminEntityOutput>() { Data = await _sysAdminRepository.Get(id) };
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("update"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<string>> Update([FromBody] SysAdminEntity input)
        {
            var model = await _sysAdminRepository.Get(input.Id);
            if (model != null)
            {
                input.Pwd = model.Pwd;
            }
            else
            {
                return new ApiResult<string>(StatusCodeEnum.Waring, false, "用户不存在");
            }
            var dbres = await _sysAdminRepository.Update(input);
            var res = new ApiResult<string>
            {
                Msg = dbres > 0 ? "修改成功" : "修改失败"
            };
            return res;
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("resetPwd"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<string>> ResetPwd([FromBody] SysAdminEntityResetPwdInput input)
        {
            var model = await _sysAdminRepository.Get(input.Id);
            if (model == null)
            {
                return new ApiResult<string>(StatusCodeEnum.Waring, false, "用户不存在");
            }

            model.Pwd = "e10adc3949ba59abbe56e057f20f883e";
            model.UpdateTime = DateTime.Now;
            model.UpdateUser = Account;

            var res = await _sysAdminRepository.Update(model);
            if (res > 0)
            {
                return new ApiResult<string>() { Msg = "密码重置成功：123456" };
            }
            return new ApiResult<string>(StatusCodeEnum.Error, false);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("updatePwd"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<string>> UpdatePwd([FromBody] SysAdminUpdatePwdInput input)
        {
            var model = await _sysAdminRepository.Get(AdminId);
            if (model == null)
            {
                return new ApiResult<string>(StatusCodeEnum.Waring, false, "用户不存在");
            }

            if (model.Pwd != input.OldPwd)
            {
                return new ApiResult<string>(StatusCodeEnum.Waring, false, "旧密码输入错误");
            }

            model.Pwd = input.NewPwd;
            model.UpdateTime = DateTime.Now;
            model.UpdateUser = Account;

            var dbres = await _sysAdminRepository.Update(model);
            var res = new ApiResult<string>
            {
                Msg = dbres > 0 ? "修改成功" : "修改失败"
            };
            return res;
        }

        /// <summary>
        /// 修改状态，禁用、启用
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("updateStatus"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<string>> UpdateStatus([FromBody] SysAdminEntityStatusInput input)
        {
            var model = await _sysAdminRepository.Get(input.Id);
            if (model == null)
            {
                return new ApiResult<string>(StatusCodeEnum.Waring, false, "用户不存在");
            }

            if (model.Status == input.Status)
            {
                return new ApiResult<string>(StatusCodeEnum.Waring, false, "状态已修改");
            }

            model.Status = input.Status;
            model.UpdateTime = DateTime.Now;
            model.UpdateUser = Account;

            var dbres = await _sysAdminRepository.Update(model);
            var res = new ApiResult<string>
            {
                Msg = dbres > 0 ? "修改成功" : "修改失败"
            };
            return res;
        }
    }
}
